//
//  MarkWindowController.m
//  iMarks
//
//  Created by Christian Speich on 18.01.08.
//  Copyright 2008 Christian Speich. All rights reserved.
//

#import "MarkWindowController.h"
#import "SubjectsManager.h"
#import "SubjectView.h"

#define MAIN_SUBJECT @"mainSubject"
#define OTHER_SUBJECT @"otherSubject"

@implementation MarkWindowController

- (void) awakeFromNib
{
	[SubjectsManager sharedInstance];
	
	[self buildSubjectViews];
	
	[myOutlineView setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleSourceList];

	[myOutlineView expandItem:MAIN_SUBJECT];
	[myOutlineView expandItem:OTHER_SUBJECT];
	SubjectView *test = [[SubjectView alloc] initWithSubjectIndex:1];
	[[[splitView subviews] objectAtIndex:1] addSubview:test];
	currentView = test;
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_updateStatus:) name:@"MarkHasChanged" object:nil];
	
	[[self window] setAutorecalculatesContentBorderThickness:YES forEdge:NSMinYEdge];
	[[self window] setContentBorderThickness:29 forEdge:NSMinYEdge];
}

- (void)buildSubjectViews
{
	NSMutableArray* keys = [[NSMutableArray alloc] initWithCapacity:1];
	NSMutableArray* objects = [[NSMutableArray alloc] initWithCapacity:1];

	NSUInteger i, count = [[[SubjectsManager sharedInstance] subjects] count] +
						  [[[SubjectsManager sharedInstance] mainSubjects] count];
	
	for (i = 0; i < count; i++) {
		Subject* subject = [[SubjectsManager sharedInstance] subjectAtIndex:i];
		[keys addObject:[[subject name] copy]];
		SubjectView* subjectView = [[SubjectView alloc] initWithSubjectIndex:i];
		[objects addObject:subjectView];
		[subject release];
	}
	
	dictionary = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
}

#pragma mark --Notifications--

- (void)_updateStatus:(NSNotification *) notification
{
	[myOutlineView reloadData];
}

#pragma mark --Delegate Methods Outline View--

- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item;
{
	if ([item isKindOfClass:[NSString class]] && 
		([item isEqualToString:MAIN_SUBJECT] || 
		 [item isEqualToString:OTHER_SUBJECT])) {
		return NO;
	}
	if (currentSelection != [item name]) {
		currentSelection = [item name];
		SubjectView* subjectView = [dictionary valueForKey:[item name]];
		if (subjectView != Nil) {
			[currentView removeFromSuperview];
			[[[splitView subviews] objectAtIndex:1] addSubview:subjectView];
			currentView = subjectView;
		}
	}
	return YES;
}

- (BOOL)outlineView:(NSOutlineView *)outlineView 
shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
	return NO;
}

- (id)outlineView:(NSOutlineView *)outlineView
objectValueForTableColumn:(NSTableColumn *)tableColumn
		   byItem:(id)item
{
	if ([[tableColumn identifier] isEqualToString:@"NameColumn"]) {
		if (item == nil) {
			return @"/";
		}
		else if ([item isKindOfClass:[NSString class]] &&
				 [item isEqualToString:MAIN_SUBJECT]) {
			return NSLocalizedString(@"mainSubjects",
									 @"Label for Main Subjects");
		}
		else if ([item isKindOfClass:[NSString class]] &&
				 [item isEqualToString:OTHER_SUBJECT]) {
			return NSLocalizedString(@"otherSubjects",
									 @"Label for other Subjects");
		}
		else {
			return [item name];
		}
	}
	else {
		if (item != nil && [item isKindOfClass:[Subject class]]) {
			return [NSString stringWithFormat:@"%1.1f", [item average]];
		}
	}
	return @" ";
}

-(BOOL)outlineView:(NSOutlineView*)outlineView isGroupItem:(id)item
{
	if ([item isKindOfClass:[NSString class]] && 
		([item isEqualToString:MAIN_SUBJECT] ||
		 [item isEqualToString:OTHER_SUBJECT])) {
		return YES;
	}
	return NO;
}

- (id)outlineView:(NSOutlineView *)outlineView
			child:(int)index
		   ofItem:(id)item
{
    if (item == nil) {
		switch (index) {
			case 0: return MAIN_SUBJECT;
			case 1: return OTHER_SUBJECT;
			default: return nil;
		}
	}
	else if ([item isKindOfClass:[NSString class]] &&
			 [item isEqualToString:MAIN_SUBJECT]) {
		return [[[SubjectsManager sharedInstance] mainSubjects] objectAtIndex:index];
	}
	else if ([item isKindOfClass:[NSString class]] && 
			 [item isEqualToString:OTHER_SUBJECT]) {
		return [[[SubjectsManager sharedInstance] subjects] objectAtIndex:index];
	}
	return nil;
}

- (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
	if (item == nil) {
		return 2;
	}
	else if ([item isKindOfClass:[NSString class]] && 
			 [item isEqualToString: MAIN_SUBJECT]) {
		return [[[SubjectsManager sharedInstance] mainSubjects] count];
	}
	else if ([item isKindOfClass:[NSString class]] && 
			 [item isEqualToString: OTHER_SUBJECT]) {
		return [[[SubjectsManager sharedInstance] subjects] count];
	}
	
    return 0;
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
	if ([item isKindOfClass:[NSString class]] && 
		([item isEqualToString:MAIN_SUBJECT] || 
		 [item isEqualToString:OTHER_SUBJECT])) {
		return YES;
	}
	return NO;
}

#pragma mark --Delegate Methods Splitt View--

- (float)splitView:(NSSplitView *)splitView 
constrainMinCoordinate:(float)proposedCoordinate ofSubviewAt:(int)index
{
	return 164.0f;
}

- (float)splitView:(NSSplitView *)splitView 
constrainMaxCoordinate:(float)proposedCoordinate ofSubviewAt:(int)index
{
	return 164.0f;
}

- (void)splitView:(NSSplitView*)sender resizeSubviewsWithOldSize:(NSSize)oldSize
{
	NSRect newFrame = [sender frame];
	NSView *left = [[sender subviews] objectAtIndex:0];
	NSRect leftFrame = [left frame];
	NSView *right = [[sender subviews] objectAtIndex:1];
	NSRect rightFrame = [right frame];
	
	CGFloat dividerThickness = [sender dividerThickness];
	
	leftFrame.size.height = newFrame.size.height;
	
	rightFrame.size.width = newFrame.size.width - leftFrame.size.width 
	- dividerThickness;
	rightFrame.size.height = newFrame.size.height;
	rightFrame.origin.x = leftFrame.size.width + dividerThickness;
	
	[left setFrame:leftFrame];
	[right setFrame:rightFrame];
}

@end
